package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> findDish(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.findDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {

        dishService.addDish(dishDTO);

        cleanCache();

        return Result.success();

    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> findDishById(@PathVariable Long id) {
        DishVO dishVO = dishService.findDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result changeDish(@RequestBody DishDTO dishDTO) {
        dishService.changeDish(dishDTO);

        cleanCache();

        return Result.success("修改成功");
    }

    /**
     * 改变起售停售状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("改变起售停售状态")
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status, Long id) {
        dishService.changeStatus(status, id);

        cleanCache();

        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @DeleteMapping
    public Result dropDish(@RequestParam List<Long> ids){
        dishService.dropDish(ids);

        cleanCache();


        return Result.success();
    }


    /**
     * 根据分类id查询相关菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询相关菜品")
    @GetMapping("/list")
    public Result findByCategoryId(Long categoryId){

        List<Dish> dishes=dishService.findByCategoryId(categoryId);
        return Result.success(dishes);
    }

    //清理Redis缓存数据
    private void cleanCache(){
        log.info("清理Redis缓存数据");
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
    }



}
